<?php
/**
 * 产品管理
 *
 */
class String {
	/**
	 * 包含中文的字符串长度，中文英文均计作1个字符
	 *
	 * @param string $str
	 * @param boolean $strip	是否去除 HTML 标记
	 * @return string
	 */
	public static function cstrlen($str = '', $strip = false){
		if($strip){
			$str = strip_tags($str);
		}
		$count = 0;
		$len = strlen($str);
		for($i=0; $i < $len; $i ++, $count ++){
			if(ord($str[$i]) >= 128){
				$i += 2;
			}
		}
		return $count;
	}

	/**
	 * 字符串截段，中文英文均计作1个字符
	 *
	 * @param string $str
	 * @param int $count
	 * @return string
	 */
	public static function csubstr($str = '', $count = 250){
		$len = strlen($str);
		$n = 0;
		for($i = 0; $i < $count; $i ++){
			if($n >= $len){
				break;
			}
			if(ord($str[$n]) > 127){
				$n += 2;
			}
			$n ++;
		}

		$out = substr($str, 0, $n);
		if($i >= $count && $len > $n){
			$out .= '...';
		}
		return $out;
	}

	/**
	 * 输出摘要
	 *
	 * @param string	$str
	 * @param int		$len
	 * @return string
	 */
	public static function summary($str = '', $len = 250){
		if(empty($str)){
			return '';
		}
		$str = stripslashes($str);
		$str = strip_tags($str);						// 去除HTML标记
		// $str = preg_replace('/\s+/', '', $str);			// 将连续2个以上的空格转化一个单个空格
		// $str = preg_replace('/　+/', '', $str);			// 将连续2个以上的空格转化一个单个空格
		// $str = preg_replace('/(&nbsp;)+/', '', $str);	// 将连续2个以上的空格转化一个单个空格
		$str = str_replace(array('&nbsp;', ' ', '　'), '', $str);
		$str = trim($str);
		$str = self::csubstr($str, $len);
		return $str;
	}

	/**
	 * 输出摘要，保留分段
	 *
	 * @param string	$str
	 * @param int		$len
	 * @return string
	 */
	public static function brief($str = '', $len = 250){
		if(empty($str)){
			return '';
		}
		$str = stripslashes($str);
		
		$str = preg_replace('/>[\s(&nbsp;)]+/', '>', $str);			// 清除空格
		$str = preg_replace('/[\s(&nbsp;)]+</', '<', $str);			// 清除空格
		$str = str_ireplace(array('</p><p>', '<br>', '<br />'), '@p@', $str);
		$str = trim(strip_tags($str));						// 去除HTML标记
		$str = self::csubstr($str, $len);
		$str = str_replace('@p@', '</p><p>', $str);
		$str = str_replace('<p></p>', '', $str);
		$str = "<p>{$str}</p>";
		return $str;
	}
	
	/**
	 * 摘要
	 *
	 * @link http://bbs.blueidea.com/thread-2674572-1-1.html
	 * @param string $text
	 * @param int $len
	 * @return string
	 */
	public static function generate_brief($text, $len = 250){
		exit('太复杂，废弃');
		mb_regex_encoding("UTF-8");
		if(mb_strlen($text) <= $len){
			return $text;
		}
		$foremost = mb_substr($text, 0, $len);
		$re = "<(\/?)(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|TABLE|TR|TD|TH|INPUT|SELECT|TEXTAREA|OBJECT|A|UL|OL|LI|BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|SPAN)[^>]*(>?)";
		$single = "/BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|BR/i";

		$Stack = array(); $posStack = array();

		mb_ereg_search_init($foremost, $re, 'i');

		while($pos = mb_ereg_search_pos()){
			$match = mb_ereg_search_getregs();
			/*
			[Child-matching Formulation]:
			$matche[1] : A "/" charactor indicating whether current "<...>" Friction is Closing Part
			$matche[2] : Element Name.
			$matche[3] : Right > of a "<...>" Friction
			*/
			if($match[1] == ""){
				$elem = $match[2];
				if(mb_eregi($single, $elem) && $match[3] != ""){
					continue;
				}
				array_push($Stack, mb_strtoupper($elem));
				array_push($posStack, $pos[0]);
			}else{
				$stackTop = $Stack[count($Stack)-1];
				$end = mb_strtoupper($match[2]);
				if(strcasecmp($stackTop,$end)==0){
					array_pop($Stack);
					array_pop($posStack);
					if($match[3] ==""){
						$foremost = $foremost.">";
					}
				}
			}
		}

		$cutpos = array_shift($posStack) - 1;
		$foremost =  mb_substr($foremost, 0, $cutpos, "UTF-8");
		return strip_tags($foremost).' ...';
	}

	/*******************************************************************************
	 * 清理Word样式
	 *******************************************************************************/

	public static function clean_style($text){
		$text = preg_replace('/<\/?font[^>]*>/i', '', $text);
		$text = preg_replace('/<span\s+style="color:\s*[^;"]+">/i', '<span>', $text);
		$text = preg_replace('/<span\s+style="([^"]+);color:\s*[^;"]+">/i', '<span style="$1">', $text);
		$text = preg_replace('/<span\s+style="color:\s*[^;]+;([^"]+)">/i', '<span style="$1">', $text);
		$text = preg_replace('/<span\s+style="([^"]+);color:\s*[^;]+;([^"]+)">/i', '<span style="$1$2">', $text);
		
		return $text;
	}

	public static function clean_word($text){
		$bIgnoreFont	= true;
		$bRemoveStyles	= true;
		
		$text = preg_replace('/<o:p>\s*<\/o:p>/i', '', $text);
		$text = preg_replace('/<o:p>[\s\S]*?<\/o:p>/i', '&nbsp;', $text);
		
		// Remove mso-xxx styles.
		$text = preg_replace('/\s*mso-[^:]+:[^;"]+;?/i', '', $text);
		// Remove margin styles.
		$text = preg_replace('/\s*MARGIN: 0cm 0cm 0pt\s*;/i', '', $text);
		$text = preg_replace('/\s*MARGIN: 0cm 0cm 0pt\s*"/i', '"', $text);
		
		$text = preg_replace('/\s*TEXT-INDENT: 0cm\s*;/i', '', $text);
		$text = preg_replace('/\s*TEXT-INDENT: 0cm\s*"/i', '"', $text);
		
		$text = preg_replace('/\s*TEXT-ALIGN: [^\s;]+;?"/i', '"', $text);
		
		$text = preg_replace('/\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/i', '"', $text);
		
		$text = preg_replace('/\s*FONT-VARIANT: [^\s;]+;?"/i', '"', $text);
		
		$text = preg_replace('/\s*tab-stops:[^;"]*;?/i', '', $text);
		$text = preg_replace('/\s*tab-stops:[^"]*/i', '', $text);
		
		// Remove FONT face attributes.
		if($bIgnoreFont){
			$text = preg_replace('/\s*face="[^"]*"/i', '', $text);
			$text = preg_replace('/\s*face=[^ >]*/i', '', $text);
			
			$text = preg_replace('/\s*FONT-FAMILY:[^;"]*;?/i', '', $text);
		}
		
		// Remove Class attributes
		$text = preg_replace('/<(\w[^>]*) class=([^ |>]*)([^>]*)/i', '<$1$3', $text);
		
		// Remove styles.
		if($bRemoveStyles){
			$text = preg_replace('/<(\w[^>]*) style="([^\"]*)"([^>]*)/i', '<$1$3', $text);
		}
		
		// Remove style, meta and link tags
		$text = preg_replace('/<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/i', '', $text);
		$text = preg_replace('/<(?:META|LINK)[^>]*>\s*/i', '', $text);
		$text = preg_replace('/\s*<\/meta>\s*/i', '', $text);
		
		// Remove empty styles.
		$text = preg_replace('/\s*style="\s*"/i', '', $text);
		$text = preg_replace('/<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/i', '&nbsp;', $text);
		$text = preg_replace('/<SPAN\s*[^>]*><\/SPAN>/i', '', $text);
		
		// Remove Lang attributes
		$text = preg_replace('/<(\w[^>]*) lang=([^ |>]*)([^>]*)/i', '<$1$3', $text);
		#$text = preg_replace('/<SPAN\s*>([\s\S]*?)<\/SPAN>/i', '$1', $text);
		$text = preg_replace('/<FONT\s*>([\s\S]*?)<\/FONT>/i', '$1', $text);
		
		// Remove XML elements and declarations
		$text = preg_replace('/<\?\?xml[^>]*>/i', '', $text);
		
		// Remove w: tags with contents.
		$text = preg_replace('/<w:[^>]*>[\s\S]*?<\/w:[^>]*>/i', '', $text);
		
		// Remove Tags with XML namespace declarations: <o:p><\/o:p>
		$text = preg_replace('/<\/?\w+:[^>]*>/i', '', $text);
		
		// Remove comments [SF BUG-1481861].
		$text = preg_replace('/<\!--[\s\S]*?-->/i', '', $text);
		$text = preg_replace('/<(U|I|STRIKE)>&nbsp;<\/\1>/i', '&nbsp;', $text);
		$text = preg_replace('/<H\d>\s*<\/H\d>/i', '', $text);
		
		// Remove "display:none" tags.
		$text = preg_replace('/<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/i', '', $text);
		
		// Remove language tags
		$text = preg_replace('/<(\w[^>]*) language=([^ |>]*)([^>]*)/i', '<$1$3', $text);
		
		// Remove onmouseover and onmouseout events (from MS Word comments effect)
		$text = preg_replace('/<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/i', '<$1$3', $text);
		$text = preg_replace('/<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/i', '<$1$3', $text);
		
		return $text;
	}

	/*******************************************************************************
	 * 格式化纯文本
	 *******************************************************************************/

	public static function format_text($text, $stripemptyline = false){
		$text = strip_tags($text);
		$text = str_replace("\n", '</p><p>', $text);
		$text = '<p>'.$text.'</p>';
		if($stripemptyline){
			$text = preg_replace('/<p>\s*<\/p>/', '', $text);
		}
		//$text = str_replace("\r", '', $text);
		return $text;
	}
}

// 测试
//header( 'Content-type:text/html; charset=utf-8');
//echo String::cstrlen('中华人民共和国BMW');
//echo "\n\n";
//
//$str = "欢迎welcome光临博客之家中华人民共和国";
//$count = 9;
//$coral = String::csubstr($str, $count);
//echo $coral;

?>